import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
import java.nio.file.*;

public class MySSLClient {

	private static final String DEFAULT_HOST = "127.0.0.1";
	private static final int DEFAULT_PORT = 7777;

	private static final String CLIENT_KEY_STORE_PASSWORD = "123456";
	private static final String CLIENT_TRUST_KEY_STORE_PASSWORD = "123456";

	private SSLSocket sslSocket;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MySSLClient client = new MySSLClient();
		client.init();
		client.process();

	}

	public void init() {
		try {
			SSLContext ctx = SSLContext.getInstance("SSL");

            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");

            KeyStore ks = KeyStore.getInstance("JKS");
            KeyStore tks = KeyStore.getInstance("JKS");

            ks.load(new FileInputStream("data/node_b_keystore.jks"), CLIENT_KEY_STORE_PASSWORD.toCharArray());
            tks.load(new FileInputStream("data/ca_keystore.jks"), CLIENT_TRUST_KEY_STORE_PASSWORD.toCharArray());

            kmf.init(ks, CLIENT_KEY_STORE_PASSWORD.toCharArray());
            tmf.init(tks);

            ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

            sslSocket = (SSLSocket) ctx.getSocketFactory().createSocket(DEFAULT_HOST, DEFAULT_PORT);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
	
	public void process() {
		if (sslSocket == null) {
			System.out.println("ERROR");
			return;
		}
		try {

			BufferedOutputStream outToClient = null;
			outToClient = new BufferedOutputStream(sslSocket.getOutputStream());
			if (outToClient != null) {
				File myFile = new File("crl.pem");
				byte[] mybytearray = new byte[(int) myFile.length()];
				byte[] bytesaa = Files.readAllBytes(myFile.toPath());
				String s = new String(bytesaa);

				System.out.println(s);

				FileInputStream fis = null;
				fis = new FileInputStream(myFile);
				BufferedInputStream bis = new BufferedInputStream(fis);
				bis.read(mybytearray, 0, mybytearray.length);
				String ss = new String(mybytearray);
				System.out.println(ss.toString());
				System.out.println("array length is " + mybytearray.length);
				outToClient.write(mybytearray, 0, mybytearray.length);
				outToClient.flush();
				outToClient.close();
				// s.close();

			}
			sslSocket.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
